package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.framework.core.result.APIListResponse;
import com.framework.core.result.APIObjResponse;
import com.framework.core.result.BasePage;
import com.project.base.BaseController;
import com.project.biz.CmsSupplierBiz;
import com.project.common.model.UserInfo;
import com.project.common.utils.IpageToPage;
import com.project.pojo.dto.SupplierDTO;
import com.project.pojo.model.AddSupplierModel;
import com.project.pojo.model.SelectSupplierModel;
import com.project.pojo.model.UpdateSupplierModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 供应商
 * @Author: zhuchi
 * @CreateDate: 2020/12/15 11:09
 * @Version: 1.0
 */
@Api(value = "CmsSupplierController", description = "供应商")
@RestController
@RequestMapping("/supplier")
@Slf4j
public class CmsSupplierController extends BaseController {

    /**
     * 供应商业务层接口类
     */
    @Autowired
    private CmsSupplierBiz supplierBiz;

    /**
     * 分页查询供应商
     * @param selectSupplierModel
     * @return
     */
    @ApiOperation(value = "分页查询供应商")
    @PostMapping(value = "/selectSupplier")
    public APIObjResponse<BasePage<SupplierDTO>> selectSupplier(@RequestBody SelectSupplierModel selectSupplierModel){
        IPage<SupplierDTO> iPage = this.supplierBiz.selectSupplier(selectSupplierModel);
        BasePage<SupplierDTO> basePage = IpageToPage.toBasePage(iPage, SupplierDTO.class);
        return APIObjResponse.success("查询成功", basePage);
    }

    /**
     * 获取供应商列表
     * @param selectSupplierModel
     * @return
     */
    @ApiOperation(value = "获取供应商列表")
    @PostMapping(value = "/selectSupplierList")
    public APIListResponse<SupplierDTO> selectSupplierList(@RequestBody SelectSupplierModel selectSupplierModel){
        List<SupplierDTO> list = this.supplierBiz.selectSupplierList(selectSupplierModel);
        return APIListResponse.success("查询成功", list);
    }

    /**
     * 添加供应商
     * @param addSupplierModel
     * @return
     */
    @ApiOperation(value = "添加供应商")
    @PostMapping(value = "/addSupplier")
    public APIObjResponse<Integer> addSupplier(@Validated @RequestBody AddSupplierModel addSupplierModel){
        UserInfo userInfo = super.getLoginUser();
        Integer result = this.supplierBiz.addSupplier(addSupplierModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 修改供应商
     * @param updateSupplierModel
     * @return
     */
    @ApiOperation(value = "修改供应商")
    @PostMapping(value = "/updateSupplier")
    public APIObjResponse<Integer> updateSupplier(@Validated @RequestBody UpdateSupplierModel updateSupplierModel){
        UserInfo userInfo = super.getLoginUser();
        Integer result = this.supplierBiz.updateSupplier(updateSupplierModel, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

    /**
     * 删除供应商
     * @param supplierId
     * @return
     */
    @ApiOperation(value = "删除供应商,参数:{ supplierId: 供应商ID }")
    @GetMapping(value = "/delSupplier")
    public APIObjResponse<Integer> delSupplier(@RequestParam("supplierId") Long supplierId){
        UserInfo userInfo = super.getLoginUser();
        Integer result = this.supplierBiz.delSupplier(supplierId, userInfo.getBaseUserId());
        return APIObjResponse.success("操作成功", result);
    }

}
